let jwt = require('jsonwebtoken');

let login = async ctx => {

  let {name, pwd} = ctx.request.body;
  let res = await ctx.db.select('user', {name});
  if (res.length === 0) {
    ctx.body = {
      code: -1,
      msg: '登录失败'
    };
  } else {
    if (res[0].pwd === pwd) {
      // 签发token
      let token = jwt.sign({name, pwd, uid: res[0].uid}, ctx.key, {expiresIn: '24h'});
      ctx.body = {
        code: 1,
        token,
        msg: '登录成功'
      };
    }
  }
};

let admin = async ctx => {
  // ctx.uid  3表联查
  // 获取对应的权限 execute
  //

  ctx.body = {code: 1, msg: 'admin'};

};

module.exports = {
  login, admin
};
